package com.ciaojian.bytedance.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ciaojian.core.bytedance.MiniAppRequestApi;
import com.ciaojian.core.bytedance.api.ByteDanceRequestApi;
import com.ciaojian.core.bytedance.auth.ByteDanceTokenProvider;
import com.ciaojian.bytedance.vo.bytedance.ByteDancePoiVo;
import com.ciaojian.bytedance.vo.bytedance.ByteDanceSyncStoreVo;
import com.ciaojian.core.bytedance.ByteDanceConfig;
import com.ciaojian.core.mapper.StoreMapper;
import com.ciaojian.core.model.Store;
import com.ciaojian.core.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class ByteDancePoiService {
    @Autowired
    private ByteDanceTokenProvider byteDanceTokenProvider;
    @Resource
    private StoreMapper storeMapper;

    /**
     * 发起店铺匹配 POI 同步任务
     */
    public JSONObject sendPoisSyncMatch(ByteDancePoiVo byteDancePoiVo) {
        String bytedance_clienttoken = byteDanceTokenProvider.getBytedanceClienttoken("BYTEDANCE_CLIENTTOKEN");

        JSONObject param = new JSONObject();
        param.put("match_data_list", byteDancePoiVo.getMatch_data_list());
        //定义接收数据
        JSONObject result = new JSONObject();

        try{
            String poi_supplier = ByteDanceRequestApi.sendRequestClienttoken(MiniAppRequestApi.POI_SUPPLIER,bytedance_clienttoken,null, param);
            result = JSON.parseObject(poi_supplier);
            return result;

        }catch (Exception e){
            return result;
        }
    }

    /**
     * 店铺匹配任务结果查询
     */
    public JSONObject querytask(String supplierTaskIds) {
        String bytedance_clienttoken = byteDanceTokenProvider.getBytedanceClienttoken("BYTEDANCE_CLIENTTOKEN");

        //定义接收数据
        JSONObject result = new JSONObject();

        try{
            String poi_query_task = ByteDanceRequestApi.sendRequestClienttoken(MiniAppRequestApi.POI_QUERY_TASK, bytedance_clienttoken, supplierTaskIds, null);
            result = JSON.parseObject(poi_query_task);
            return result;

        }catch (Exception e){
            return result;
        }
    }

    /**
     * 商铺同步
     * @param byteDanceSyncStoreVo
     * @return
     */
    public JSONObject syncStore(ByteDanceSyncStoreVo byteDanceSyncStoreVo) {
        String bytedance_clienttoken = byteDanceTokenProvider.getBytedanceClienttoken("BYTEDANCE_CLIENTTOKEN");

        JSONObject param = new JSONObject();
        param.put("poi_id", byteDanceSyncStoreVo.getPoi_id());
        param.put("supplier_ext_id", byteDanceSyncStoreVo.getSupplier_ext_id());
        param.put("type", byteDanceSyncStoreVo.getType());
        param.put("status", byteDanceSyncStoreVo.getStatus());
        param.put("name", byteDanceSyncStoreVo.getName());
        param.put("attributes", byteDanceSyncStoreVo.getAttributes());
        //定义接收数据
        JSONObject result = new JSONObject();

        try{

            String poi_sync_store = ByteDanceRequestApi.sendRequestClienttoken(MiniAppRequestApi.POI_SYNC_STORE,bytedance_clienttoken,null, param);

            result = JSON.parseObject(poi_sync_store);

            return result;

        }catch (Exception e){
            return result;
        }
    }

    /**
     * 查看商铺
     * @param supplierExtId
     * @return
     */
    public JSONObject querystore(String supplierExtId) {
        String bytedance_clienttoken = byteDanceTokenProvider.getBytedanceClienttoken("BYTEDANCE_CLIENTTOKEN");

        //定义接收数据
        JSONObject result = new JSONObject();

        try{
            String poi_query_store = ByteDanceRequestApi.sendRequestClienttoken(MiniAppRequestApi.POI_QUERY_STORE, bytedance_clienttoken, supplierExtId, null);
            result = JSON.parseObject(poi_query_store);
            return result;

        }catch (Exception e){
            return result;
        }
    }

    /**
     * 应用未申请 获取抖音店铺poi列表
     * @return
     * @throws Exception
     */
    public JSONObject storePoi() {

        String bytedance_clienttoken = byteDanceTokenProvider.getBytedanceClienttoken("BYTEDANCE_CLIENTTOKEN");

        //定义接收数据
        JSONObject result = new JSONObject();

        try{
            HttpGet httpGet = new HttpGet(ByteDanceConfig.STORE_LIST_URL);
            CloseableHttpClient client = HttpClients.createDefault();

            httpGet.setHeader("access-token",bytedance_clienttoken);

            HttpResponse response = client.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == 200) {
                result = JSON.parseObject(EntityUtils.toString(response.getEntity(), "UTF-8"));
            }
            return result;

        }catch (Exception e){
            return result;
        }
    }

    /**
     * 获取店铺列表
     * @return
     */
    public Result storeList() {
        QueryWrapper<Store> qw = new QueryWrapper<>();
        qw.eq("state",1);
        qw.eq("is_hidden",0);
        qw.eq("business_type", 0);
        qw.eq("is_del", 0);

        List<Store> stores = storeMapper.selectList(qw);

        return Result.ok(stores);
    }
}
